import dayjs from "dayjs";
import rt from 'dayjs/plugin/relativeTime'
// 语言包
import 'dayjs/locale/zh-cn'
import store from '@/store'
export const dateFilter = (val, format = "YYYY-MM-DD") => {
    if (!isNaN(val)) {
        // 如果val可以被转成数字
        val = parseInt(val);
    }

    return dayjs(val).format(format);
};

dayjs.extend(rt)
function relativeTime(val) {
    if (!isNaN(val)) {
        val = parseInt(val)
    }
    // dayjs().to(dayjs(val))意思是当前时间相对于传入时间的时间
    return dayjs()
        .locale(store.getters.language === 'zh' ? 'zh-cn' : 'en')
        .to(dayjs(val))
}

export default app => {
    app.config.globalProperties.$filters = {
        dateFilter,
        relativeTime
    };
};